package com.robot.util;

import com.robot.vo.ReportLog;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/12/8
 * Time: 15:05
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
public class ReportLogUtil {
    private OutputStreamWriter writer;
    private FileOutputStream fileOutputStream;
    public ReportLogUtil(){}
    public ReportLogUtil(Integer buildTestId){
        try {
            File file =new File(buildTestId+".log");
            System.out.println(file.getAbsolutePath());
             this.fileOutputStream=new FileOutputStream(file);
            this.writer=new OutputStreamWriter(fileOutputStream,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }catch (FileNotFoundException f){

        }
    }
    /**
     * 输出红色日志
     * **/
    public void writeRedSpan(String msg){
        String span ="<span style='color:red'>"+msg+"</span>";
        this.write(span);
    }
    /**
     * 输出绿色日志
     * **/
    public void writeGreenSpan(String msg){
        String span ="<span style='color:green'>"+msg+"</span>";
        this.write(span);
    }
    /**
     * 输出普通日志
     * **/
    public void writeSpan(String msg){
        String span ="<span>"+msg+"</span>";
        this.write(span);
    }
    private void write(String msg){
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("[");
        DateUtil dateUtil =new DateUtil();
        stringBuffer.append(dateUtil.getCurrentTime());
        stringBuffer.append("]");
        stringBuffer.append(" -->");
        stringBuffer.append(msg);
        try {
            writer.write("<div>"+stringBuffer.toString()+"\r\n</div>");
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void closeWriter(){
        try{
            this.fileOutputStream.close();
            this.writer.close();
        }catch (IOException i){

        }
    }
    /**记录每次文件读取的长度，下次读取会接着上次继续读取**/
    public ReportLog read(Integer buildTestId, Long lastTimeFileSize){
        ReportLog reportLog=new ReportLog();
        try{
            String filePath = buildTestId+".log";
            /**只读模式**/
            RandomAccessFile  randomAccessFile =new RandomAccessFile(filePath,"r");
            /**记录从哪里开始读取这个文件**/
            randomAccessFile.seek(lastTimeFileSize);
            String temp =null;
            StringBuffer stringBuffer=new StringBuffer();
            while ((temp=randomAccessFile.readLine())!=null){
                String text =new String(temp.getBytes("ISO-8859-1"),"UTF-8");
                stringBuffer.append(text);
            }
            /**记录了这次读取到的长度**/
            lastTimeFileSize =randomAccessFile.length();
            reportLog.setLastTimeFileSize(lastTimeFileSize);
            reportLog.setReportText(stringBuffer.toString());
        }catch (FileNotFoundException f){

        }catch (IOException e){

        }
        return reportLog;
    }
}
